package com.example.yskjproject.ui.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.example.yskjproject.base.BaseActivity;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.databinding.ActivityHeightAlertBinding;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.utils.EventBusUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HeightAlertActivity extends BaseActivity<ActivityHeightAlertBinding> {
    ActivityHeightAlertBinding mActivityHeightAlertBinding;
    private Gson gson;

    @Override
    protected ActivityHeightAlertBinding getViewBinding() {
        mActivityHeightAlertBinding = ActivityHeightAlertBinding.inflate(LayoutInflater.from(this));
        return mActivityHeightAlertBinding;
    }

    @Override
    protected boolean shouldShowTitle() {
        return false;
    }

    @Override
    protected void initData() {
        mActivityHeightAlertBinding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mActivityHeightAlertBinding.tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(mActivityHeightAlertBinding.heightEditText.getText())) {
                    gson = new Gson();
                    saveHeightInfo();
                }
            }
        });
    }

    private void saveHeightInfo() {
        Map<String, Object> jsonParams = new HashMap<>();
        jsonParams.put("height", Integer.parseInt(mActivityHeightAlertBinding.heightEditText.getText().toString()));
        String paramsDate = gson.toJson(jsonParams);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), paramsDate);
        OkGo.<LzyResponse<Void>>post(Urls.UPDATE_USER).upRequestBody(requestBody).execute(new JsonCallback<LzyResponse<Void>>() {
            @Override
            public void onSuccess(Response<LzyResponse<Void>> response) {
                ToastUtil.toastLongMessage("修改身高成功");
                EventMessage eventMessage = new EventMessage(EventContent.update_height, mActivityHeightAlertBinding.heightEditText.getText().toString());
                EventBusUtil.sendEvent(eventMessage);
                finish();
            }

            @Override
            public void onError(Response<LzyResponse<Void>> response) {
                super.onError(response);
            }
        });
    }
}
